package com.mhy.anno.mapper.impl;

import com.mhy.anno.mapper.BookMapper;
import jdk.nashorn.internal.objects.annotations.Constructor;
import lombok.ToString;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.sql.DataSource;
import java.sql.*;

@Repository("bookMapper")
@Scope
@ToString
public class BookMapperImpl implements BookMapper {
    @Autowired
    DataSource dataSource;

    public BookMapperImpl(){
        System.out.println("Construct");
    }
    @Override
    public String selectById(Integer id) throws SQLException {
        ResultSet rs =null;
        Statement stmt = null;
        Connection conn = null;
        try{
            conn = dataSource.getConnection();
            System.out.println("conn "+conn);
            stmt = conn.createStatement();
            System.out.println("stmt "+stmt);
            String sql = "select * from user";
            rs = stmt.executeQuery(sql);
            System.out.println("rs "+rs);
            while (rs.next()) {
                System.out.println(rs.getInt("id") + " " + rs.getString("username"));
            }
        }catch (SQLException e){

        }finally {
            rs.close();
            stmt.close();
            conn.close();
        }
        System.out.println("selectById");
        return "book";
    }




    @PostConstruct
    public void init(){
        System.out.println("init");
    }

    @PreDestroy
    public void destroy(){
        System.out.println("destroy");
    }
}
